"""
发送post请求
"""
import requests


class YouDao:
    # 初始化，构造对象时会自动调用它
    def __init__(self, word):
        self.url = 'http://fanyi.youdao.com/?keyfrom=dict2.top'
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
        }
        # 要翻译的词，变化的，传递过来的
        self.word = word
        self.data = {
            'i': self.word,
            'from': 'AUTO',
            'to': 'AUTO',
            'smartresult': 'dict',
            'client': 'fanyideskweb',
            'salt': '16057688207108',
            'sign': '65596d3130f68a92c87c022de2fae9d0',
            'lts': '1605768820710',
            'bv': 'f7d97c24a497388db1420108e6c3537b',
            'doctype': 'json',
            'version': '2.1',
            'keyfrom': 'fanyi.web',
            'action': 'FY_BY_CLICKBUTTION'
        }

        # 爬取数据的函数
    def get_data(self):
        result = requests.post(self.url, headers=self.headers, data=self.data)
        return result.content.decode()

fan1=YouDao('hello')
print(fan1.get_data())
fan2=YouDao('好好学习')
print(fan2.get_data())